﻿using System;

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Site3AI.Extensions
{
    public class SiteMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        //protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
        //{
        //    var r= base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        //    if (propertyName != null)
        //    {
        //        if (r.DisplayName == null)
        //        {
        //            r.DisplayName =App_LocalResources.ForMetadata.ResourceManager.GetString(containerType.Name + "_" + r.PropertyName)
        //                ?? System.Text.RegularExpressions.Regex.Replace(r.PropertyName, "([A-Z][a-z]?)", " $1").Trim();
        //        }
        //    }
        //    return r;
        //}

        protected override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        {
            var r= base.GetMetadataForProperty(modelAccessor, containerType, propertyDescriptor);
           if (r.DisplayName == null) { 
                if(r.PropertyName!=null){
                    r.DisplayName = System.Text.RegularExpressions.Regex.Replace(r.PropertyName, "([A-Z][a-z]?)"," $1").Trim();
                }
            }
            return r;
        }
    }
}